package cn.wolfcode.shop.cloud.redis;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.msg.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

public class RedisService {
    @Autowired
    private JedisPool jedisPool;
    //set
    public <T>void set(KeyPrefix keyPrefix,String key,T data){
        try (Jedis jedis = jedisPool.getResource()){
            //判断是否有过期时间
            String realKey = keyPrefix.getKeyPrefix() + key;
            if (keyPrefix.getExpireTime()>0){
                jedis.setex(realKey, keyPrefix.getExpireTime(), JSON.toJSONString(data));
            }else {
             jedis.set(realKey, JSON.toJSONString(data));
            }
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
    //get
    public <T>T get(KeyPrefix keyPrefix,String key,Class<T> clz){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            String realVal = jedis.get(realKey);
            if (StringUtils.isEmpty(realVal)){
               return null;
            }else {
                return JSON.parseObject(realVal, clz);
            }
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //incr
    public long incr(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.incr(realKey);
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //decr
    public long decr(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.decr(realKey);
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //exists
    public boolean exists(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.exists(realKey);
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
    //expire
    public long expire(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.expire(realKey,keyPrefix.getExpireTime());
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hset
    public <T>void hset(KeyPrefix keyPrefix,String key,String field,T data){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            jedis.hset(realKey, field, JSON.toJSONString(data));
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hget
    public <T>T hget(KeyPrefix keyPrefix,String key,String field,Class<T> clz){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            String redisVal = jedis.hget(realKey, field);
            if (redisVal == null){
                return null;
            }
            return JSON.parseObject(redisVal,clz);
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
    //hgetAll
    public <T>Map<String,T> hgetAll(KeyPrefix keyPrefix, String key,Class<T> clz){
        try (Jedis jedis = jedisPool.getResource()){
            String realKey = keyPrefix.getKeyPrefix() + key;
            Map<String, String> stringStringMap = jedis.hgetAll(realKey);
            Map<String,T> resultMap = new HashMap<>();
            for (Map.Entry<String,String> entry : stringStringMap.entrySet()) {
                resultMap.put(entry.getKey(), JSON.parseObject(entry.getValue(),clz));
            }

            return resultMap;
        } catch(Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
}
